#include <iostream>
#include <sstream>

#include <strsplit.hh>

namespace Mage {

bool
strsplit(const std::string& str, std::vector<std::string>& res, char sep,
         std::size_t max)
{
    res.clear();
    std::istringstream iss(str);

    // tokenize
    for (std::string tmp;
         res.size() < max && iss.good() && std::getline(iss, tmp, sep);
         res.push_back(std::move(tmp)))
        ;

    // push in any left overs
    if (iss.good()) {
        std::string tmp;
        std::getline(iss, tmp, '\0');
        res.push_back(std::move(tmp));
    }
    
    return true;
}

} // namespace Mage
